package org.apache.ocean.main.search;

import java.io.IOException;
import java.util.concurrent.atomic.AtomicInteger;

import org.apache.lucene.document.Document;
import org.apache.lucene.document.FieldSelector;
import org.apache.lucene.index.Term;
import org.apache.lucene.index.TermFreqVector;
import org.apache.lucene.search.Explanation;
import org.apache.lucene.search.Filter;
import org.apache.lucene.search.HitCollector;
import org.apache.lucene.search.Query;
import org.apache.lucene.search.Searcher;
import org.apache.lucene.search.Similarity;
import org.apache.lucene.search.Sort;
import org.apache.lucene.search.TopDocs;
import org.apache.lucene.search.TopFieldDocs;
import org.apache.lucene.search.Weight;


/**
 *
 * @author jasonr
 */
public class CachedDocumentFrequencySearcher extends Searcher {
  private TermFrequencies termFrequencies; // Map from Terms to corresponding doc freqs
  private int maxDoc; // document count
  
  public CachedDocumentFrequencySearcher(TermFrequencies termFrequencies, Similarity similarity) {
    this.termFrequencies = termFrequencies;
    this.maxDoc = termFrequencies.getMaxDoc();
    setSimilarity(similarity);
  }
  
  public int docFreq(Term term) {
    int freq;
    try {
    	AtomicInteger value = termFrequencies.get(term);
    	if (value == null) return 0;
      freq = value.intValue();
    } catch (NullPointerException e) {
      throw new IllegalArgumentException("df for term " + term.text()+ " not available");
    }
    return freq;
  }
  
  public int[] docFreqs(Term[] terms) {
    int[] result = new int[terms.length];
    for (int i = 0; i < terms.length; i++) {
      result[i] = docFreq(terms[i]);
    }
    return result;
  }
  
  public int maxDoc() {
    return maxDoc;
  }
  
  public Query rewrite(Query query) {
    // this is a bit of a hack. We know that a query which
    // creates a Weight based on this Dummy-Searcher is
    // always already rewritten (see preparedWeight()).
    // Therefore we just return the unmodified query here
    return query;
  }
  
  public void close() {
    throw new UnsupportedOperationException();
  }
  
  public Document doc(int i) {
    throw new UnsupportedOperationException();
  }
  
  public TermFreqVector getTermFreqVector(int docNumber, String field) throws IOException {
    throw new UnsupportedOperationException();
  }
  
  public Document doc(int i, FieldSelector fieldSelector) {
    throw new UnsupportedOperationException();
  }
  
  public Explanation explain(Weight weight,int doc) { throw new UnsupportedOperationException(); }
  
  public void search(Weight weight, Filter filter, HitCollector results) { throw new UnsupportedOperationException(); }
  
  public TopDocs search(Weight weight,Filter filter,int n) { throw new UnsupportedOperationException(); }
  
  public TopFieldDocs search(Weight weight,Filter filter,int n,Sort sort) {
    throw new UnsupportedOperationException();
  }
}